package com.sgx.learning.luban.io.bio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9000);
        while (true) {
            System.out.println("等待连接");
            Socket accept = serverSocket.accept();
            System.out.println("有客户端连接");
            handler(accept);
        }
    }

    private static void handler(Socket accept) throws IOException {
        byte[] b = new byte[1024];
        System.out.println("开始read..");
        int read = accept.getInputStream().read(b);
        if(read != -1) {
            System.out.println("收到客户端的数据:" + new String(b,0,read));
        }
        accept.getOutputStream().write("hello client".getBytes());
        accept.getOutputStream().flush();
    }
}
